package sg.edu.nus.iss.jass.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComponent;

import sg.edu.nus.iss.jass.dao.JobAdvertDAO;
import sg.edu.nus.iss.jass.dao.ResponseDAO;
import sg.edu.nus.iss.jass.gui.ViewResponseDetailPanel;
import sg.edu.nus.iss.jass.gui.ViewResponseListPanel;
import sg.edu.nus.iss.jass.model.CurrentUserDTO;
import sg.edu.nus.iss.jass.model.ResponseDTO;

/**
 * This class object co-ordinates the Response related functions by the Advertiser. It receives request to perform the following functions:
 * <br/> - View Response made against the Job Advert
 * <br/> - Initialize View Response Screen
 * 
 * <p>It manages the interaction between the response screens and the Data Access Object(DAO).</p>
 * 
 * @author SE19 Team 1
 * @version 1.0
 */	
public class ResponseController implements ActionListener {

	private MainMenuController mainMenuController;
	private ViewResponseListPanel viewResponseListPanel;
	private ViewResponseDetailPanel viewResponseDetailPanel;
	private ResponseDAO responseDAO;
	private JobAdvertDAO jobAdvertDAO;

	/**
	 * This operation is the default constructor of ResponseController.&nbsp;The parameters of the operation 
	 * will define the initial value for the attribute of the controller.
	 * 
	 * @param mainMenuController A reference to the main menu controller.
	 */
	public ResponseController(MainMenuController mainMenuController) {
		this.mainMenuController = mainMenuController;
		this.responseDAO = new ResponseDAO();
		this.jobAdvertDAO = new JobAdvertDAO();
		this.viewResponseDetailPanel = new ViewResponseDetailPanel();
		this.viewResponseListPanel = new ViewResponseListPanel();
	}

	/**
	 * This operation is implementing the ActionListener interface.&nbsp;It will listen to the event of the button 
	 * and invokes the respective operation to complete a request.
	 * 
	 * @param e A semantic event which indicates that a component-defined action occured.
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		String button = ((JComponent)e.getSource()).getName();
		
		if (ViewResponseListPanel.BTN_VIEW_RESPONSE.equals(button)) {
			// Exception has to manage if the user is not selecting any of the
			// response from the response list.
			try {
				String responseId = this.viewResponseListPanel.getSelectedResponse();
				this.initViewResponseDetailScreen(responseId);
			} catch (IllegalArgumentException ie) {
				this.initViewResponseListScreen(this.mainMenuController.getCurrentUser());
				this.viewResponseListPanel.setMessage(ie.getMessage());
			}
		}
		else if (ViewResponseDetailPanel.BTN_BACK_TO_RESPONSE_LIST.equals(button)) {
			this.initViewResponseListScreen(this.mainMenuController.getCurrentUser());
		}
	}

	/**
	 * This operation is initiates when the user(Advertiser) selects on one of the responses on the View
	 * Response Screen.&nbsp;It will perform the following steps:<br/>
	 * 		<br/>1) Retrieve the selected response If from the form.&nbsp;
	 * 		<br/>2) Retrieve the response detail from the response data source (i.e.&nbsp;response.properties) and associated job advert detail from the job advert data source (i.e.&nbsp;jobadvert.properties).&nbsp;
	 * 		<br/>3) Forward the completed request to display View Response Detail Screen.&nbsp;
	 * 
	 * @param responseId An unique Id that represents the selected response.
	 */
	public void initViewResponseDetailScreen(String responseId) {

		ResponseDTO response = this.responseDAO.getResponse(responseId);
		response.setJobAdvert(this.jobAdvertDAO.getJobAdvert(response.getJobAdvertId()));
		if (response != null) {
			this.viewResponseDetailPanel.setResponse(response);
			this.viewResponseDetailPanel.addComponentActionListener(this);
			this.mainMenuController.switchPage(this.viewResponseDetailPanel);
		}
	}

	/**
	 * This operation is initiates when the user(Advertiser) clicks on the Response link on the Advertiser 
	 * Main Menu.&nbsp;It will perform the following steps:<br/>
	 * 		<br/>1) Retrieve the logged on userId from the CurrentUserDTO object.&nbsp;
	 * 		<br/>2) Retrieve the list of responses from the response data source (i.e.&nbsp;response.properties) and job advert details from the job advert data source (i.e.&nbsp;jobadvert.properties).&nbsp;
	 * 		<br/>3) Forward the completed request to View Response List Screen.&nbsp;
	 * 
	 * @param currentUser A Data Transfer Object(DTO) that holds the current logon user information.
	 */
	public void initViewResponseListScreen(CurrentUserDTO currentUser) {

		List<ResponseDTO> responsesForCurrentAdvertiser = new ArrayList<ResponseDTO>();
		List<ResponseDTO> responses = this.responseDAO.getResponseList();

		for (ResponseDTO response : responses) {
			response.setJobAdvert(this.jobAdvertDAO.getJobAdvert(response.getJobAdvertId()));
			if (response.getJobAdvert().getStatus()!=2 &&
				!response.getState().equalsIgnoreCase("Inactive") &&
				response.getJobAdvert().getAdvertiser().equalsIgnoreCase(currentUser.getUserId()))
				responsesForCurrentAdvertiser.add(response);
		}

		this.viewResponseListPanel.setResponseList(responsesForCurrentAdvertiser);
		this.viewResponseListPanel.addComponentActionListener(this);
		this.mainMenuController.switchPage(this.viewResponseListPanel);
	}
	
	/**
	 * This operation will set the Job Advert DAO for response controller.
	 * 
	 * @param jobAdvertDAO A Data Access Object(DAO) for job advert to access the job advert data-source.
	 */
	protected void setJobAdvertDAO(JobAdvertDAO jobAdvertDAO) {
		this.jobAdvertDAO = jobAdvertDAO;
	}
	
	/**
	 * This operation will set the Response DAO for response controller.
	 * 
	 * @param responseDAO A Data Access Object(DAO) for response to access the response data-source.
	 */
	protected void setResponsedDAO(ResponseDAO responseDAO) {
		this.responseDAO = responseDAO;
	}
	
	/**
	 * This operation will set the user interface for response details.
	 * 
	 * @param viewResponseDetailPanel User interface to display response details.
	 */
	protected void setViewResponseDetailPanel(ViewResponseDetailPanel viewResponseDetailPanel) {
		this.viewResponseDetailPanel = viewResponseDetailPanel;
	}
	
	/**
	 * This operation will set the user interface for response listing.
	 * 
	 * @param viewResponseListPanel User interface to display response listing.
	 */
	protected void setViewResponseListPanel(ViewResponseListPanel viewResponseListPanel) {
		this.viewResponseListPanel = viewResponseListPanel;
	}
}
